﻿using Microsoft.AspNet.Identity;
using System.Security.Claims;
using System.Web.Mvc;
using System;
namespace Sharp.Services.OAuth.Controllers
{
    /// <summary>
    /// 认证（authentication）：认证;身份验证;证明，鉴定;密押。
    /// </summary>
    public class AuthenticationController : Controller
    {
        ///// <summary>
        ///// 获取当前用户令牌的方法，当然前提是必须先登录成功
        ///// </summary>
        ///// <returns></returns>
        //[System.Web.Http.Authorize]
        //public string GetUserToken()
        //{
        //    //using (TokenManager tm = new TokenManager(User.Identity.Name, Session.SessionID))
        //    //{
        //    //    var token = tm.TakeToken();
        //    //    return Content(token.AccessToken);
        //    //}
        //    return "";
        //}

        //[System.Web.Http.HttpGet]
        //public string  Index(string accessToken)
        //{
        //    return "test";
        //}


        [HttpGet]
        public ActionResult Validate(string accessToken)
        {
            var claimIdentity = User.Identity as ClaimsIdentity;
            var username = User.Identity.Name;
            var userId = claimIdentity.FindFirstValue(ClaimTypes.NameIdentifier);

            return Json(new[] {
                userId.IsNullOrEmpty() ? "" : userId,
                username,
                claimIdentity.AuthenticationType,
                claimIdentity.IsAuthenticated.ToString() }, JsonRequestBehavior.AllowGet);
        }
    }
}